﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;
using System.IO;
using System.Drawing;

namespace Portal_KonTum.Admin
{
    public partial class CommentNew : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["ID"] = null;
            Session["ID"] = Request.QueryString["ID"];

            if (!IsPostBack)
            {
                if (Session["ID"] == null)
                {

                }
                else
                {
                    bin_edit();
                }
            }
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            // sua doi
            Guid gd = MethodCommon.Get_Guid(Session["ID"].ToString());
            iq.CM_EditComment(set_CM(gd));
            Response.Redirect("CommentMn.aspx?Notes=StrEdit&Active=true");
        }

        protected void bt_cancel_Click(object sender, EventArgs e)
        {

        }

        void Fill_DataEdit(Comment bg)
        {
            txt_title.Text = bg.ComTitle;
            txt_adress .Text = bg.Address;
            CKEditor1.Text = bg.ComReply;
            txt_email.Text = bg.Email;
            txt_fullname.Text = bg.FullName;
            txt_phone.Text = bg.Phone;
            chk_hienthi.Checked = bg.Active;
            txt_comcontent.Text = bg.ComContent;
     
        }

        void bin_edit()
        {
            try
            {
                Comment bg = new Comment();
                iq = QueryMgr.GetDbInstance();
                bg = iq.CM_GetCommentByID(new Guid(Session["ID"].ToString()));
                Fill_DataEdit(bg);
            }
            catch(Exception ex)
            {
                Alert.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Sửa chữa bug
        /// </summary>
        /// <param name="bg"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Comment set_CM(Guid id)
        {
            Comment bg = new Comment();
            string iduser = Session["IDName"].ToString();

            bg.CheckedBy = MethodCommon.Get_Guid(iduser);//người dùng tạo
            bg.CheckedDate = DateTime.Now;
            bg.IsDeleted = false;
            bg.Active = chk_hienthi.Checked;
            bg.Address = txt_adress.Text;
            bg.ComReply = CKEditor1.Text;
            bg.ComTitle = txt_title.Text;
            bg.Email = txt_email.Text;
            bg.FullName = txt_fullname.Text;
            bg.ComContent = txt_comcontent.Text;
            bg.Phone = txt_phone.Text;
            
            if (id != null) //sửa chữa
                bg.ComID = id;
            return bg;
        }
    }
}